﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using InternshipManagement.Models;

namespace InternshipManagement.Helpers
{
    public class ActiveSemester : System.Web.Mvc.ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            AccountRepository accountRepository = new AccountRepository();
            SemesterRepository semesterRepository = new SemesterRepository();
            Student student = accountRepository.GetStudent((int)filterContext.HttpContext.Session["UserID"]);
            int activeSemesterID = semesterRepository.GetActiveSemester().SemesterID;
            if (student.Course.SemesterID != activeSemesterID)
            {
                filterContext.Controller.ViewBag.Message = "You are not registered for the active semester.";
                filterContext.Result = new ViewResult
                                       {
                                           ViewName = "ShowMessage",
                                           ViewData = filterContext.Controller.ViewData
                                       };
            }
            base.OnActionExecuted(filterContext);
        }
    }
}